#!/usr/bin/python
"""
 * Glassbead Window Manager
 *
 * Copyright (C) 2010 Darshan Trivedi
 *
"""
from PyQt4 import QtGui,QtCore
import sys
from Util.util import debugmsg
from Xlib import X,display
from EWMHManager import EWMHManager
from KeyBindingsManager import KeyBindingsManager
from VirtualDesktopManager import VirtualDesktopManager
from CommandExecutor import CommandExecutor
from ClientManager import ClientManager


class WM:
    def __init__(self):
        print "started............"
        self.CLIENTS = {}
        self.qapp=QtGui.QApplication(sys.argv)
        self.qev=QtCore.QEventLoop()
        self.qev.wakeUp()
        self.disp = display.Display()
        self.EWMH = EWMHManager(self.disp)
        debugmsg("INITIALIZING DISPLAY")
        debugmsg("BINDING KEYS")
        defaultScreen = self.disp.screen(
                                         self.disp.get_default_screen()
                                         )
        self.root = defaultScreen.root
        geometrydata = self.root.get_geometry()._data
        self.EWMH.setEWMHworkarea(geometrydata['x'], geometrydata['y'], geometrydata['width'], geometrydata['height'])
        self.root.change_attributes(
                                    event_mask=X.SubstructureNotifyMask | \
                                    X.EnterWindowMask | \
                                    X.LeaveWindowMask | \
                                    X.StructureNotifyMask
                                    )
        self.disp.sync()        
        #INITIALIZE SOME EWMH PROPERTIES
        self.EWMH.setEWMHdesktopcount(self.NUMBER_OF_DESKTOPS)
        self.EWMH.setEWMHcurrentdesktop(self.currentdesktop - 1)

    def RunMainLoop(self):
        debugmsg("ENTERING MAIN EVENT LOOP")                            
        while(True):
            ev = self.disp.next_event()
            self.HandleEvent(ev)
            self.qev.processEvents()
    
            

    def HandleEvent(self, ev):
        debugmsg("ENTERING HANDLEEVENT with type %s" % ev.__class__)
        if(ev.type == X.MappingNotify):
            self.HandleKeyBoardMapChange(ev)
        if(ev.type == X.MapNotify):
            self.HandleMapRequest(ev)
        if(ev.type == X.CreateNotify):
            self.HandleCreateNotify(ev)
        if(ev.type == X.UnmapNotify):
            self.HandleUnMapNotify(ev)
        if(ev.type == X.DestroyNotify):
            self.HandleDestroyNotify(ev)    
        if(ev.type == X.MapRequest):
            self.HandleMapRequest(ev)        
        if(ev.type == X.KeyPress):
            self.HandleKeyPress(ev)
        if(ev.type == X.EnterNotify):
            self.HandleEnterNotify(ev)
        if(ev.type == X.ClientMessage):
            self.HandleClientMessage(ev)
        
    def DisableMasks(self):
        self.root.change_attributes(
                                    event_mask=X.SubstructureNotifyMask | \
                                    X.StructureNotifyMask
                                    )
        
    def EnableMasks(self):
        self.root.change_attributes(
                                    event_mask=X.SubstructureNotifyMask | \
                                    X.EnterWindowMask | \
                                    X.LeaveWindowMask | \
                                    X.StructureNotifyMask
                                    )
                    
 
                      
class glassbeadWM(WM, KeyBindingsManager, CommandExecutor, ClientManager, VirtualDesktopManager):
    def __init__(self):
        """This is a mixin class which inherits from different classes that make 
        up our window manager
        This order of class initialization should not be changed since it 
        makes sure that properties needed by the subsequently initialized classes
        are already present  
        """
        VirtualDesktopManager.__init__(self)
        WM.__init__(self)
        KeyBindingsManager.__init__(self)
        CommandExecutor.__init__(self)
        ClientManager.__init__(self)
           


if __name__ == "__main__":
    myWM = glassbeadWM()
    myWM.RunMainLoop()
    
    
    
